/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package org.amarena2d.examples.scenes;

import com.android1.amarena2d.nodes.FpsLabel;
import com.android1.amarena2d.nodes.Scene;
import com.android1.amarena2d.nodes.SimpleLabel;
import com.android1.amarena2d.nodes.sprites.SharedSprite;
import com.android1.amarena2d.nodes.ui.Button;
import com.android1.amarena2d.nodes.ui.ToggleButton;
import com.android1.amarena2d.texture.ManagedTexture;
import com.android1.amarena2d.texture.TextureFrame;
import com.android1.amarena2d.texture.TextureFrameImpl;
import com.android1.amarena2d.texture.Textures;
import com.badlogic.gdx.graphics.Color;
import org.amarena2d.examples.Example;

@Example(name = "Buttons", category = "Interface")
public class ButtonsExampleScene extends Scene {


    SimpleLabel buttonLabel;
    SimpleLabel toggleButtonLabel;
    SimpleLabel inactiveButtonLabel;
    SimpleLabel log;


    public ButtonsExampleScene() {

          engine.getRenderer().setClearColor(Color.WHITE);

        //place the default FPS counter into the screen
        add(new FpsLabel(10, 20, Color.BLACK));

        ManagedTexture managedTexture = Textures.$("testbuttons.png").get();
        TextureFrame active = new TextureFrameImpl(managedTexture, 0, 0, 128, 64);
        TextureFrame pressed = new TextureFrameImpl(managedTexture, 0, 64, 128, 64);
        TextureFrame disabeld = new TextureFrameImpl(managedTexture, 0, 128, 128, 64);
        TextureFrame selected = new TextureFrameImpl(managedTexture, 0, 192, 128, 64);

        SharedSprite s1 = new SharedSprite(active, 100, 200);
        SharedSprite s2 = new SharedSprite(active, 100, 130);
        SharedSprite s3 = new SharedSprite(active, 100, 60);


        buttonLabel = new SimpleLabel("Button: State = Active ", 240, 230, Color.BLACK);

        Button button = new Button(s1);
        button.setPressedFrame(pressed);
        button.setListener(new LogListener(buttonLabel));
        button.setTag("Button");

        add(button, buttonLabel);


        toggleButtonLabel = new SimpleLabel("ToggleButton: State = Active ", 240, 160, Color.BLACK);

        ToggleButton toggleButton = new ToggleButton(s2);
        toggleButton.setPressedFrame(pressed);
        toggleButton.setSelectedFrame(selected);
        toggleButton.setListener(new LogListener(toggleButtonLabel));
        toggleButton.setTag("ToggleButton");

        add(toggleButton, toggleButtonLabel);



        inactiveButtonLabel = new SimpleLabel("Button: State = Disabled ", 240, 90, Color.BLACK);

        Button disabledButton = new Button(s3);
        disabledButton.setDisabledFrame(disabeld);
        disabledButton.setStateDisabled();

        add(disabledButton, inactiveButtonLabel);

        log = new SimpleLabel(240, 50, Color.BLACK);
        add(log);


    }

    private final class LogListener extends Button.Listener {
        SimpleLabel simpleLabel;

        private LogListener(SimpleLabel simpleLabel) {
            this.simpleLabel = simpleLabel;
        }

        @Override
        public void onStateChange(Button button, int state) {
            this.simpleLabel.setText(button.getTag() + ": State = " + button.getStateString());
        }
    }

}